import InfoDialog from './InfoDialog'
import GameControl from './GameControl'
import Scoreboard from './Scoreboard'
type GameMode = 'simple' | 'advance' | 'difficult'
enum modeMap {
  simple = '简单',
  advance = '进阶',
  difficult = '地狱'
}

class ModeBoard {
  private simpleModeEle: HTMLElement
  private advanceModeEle: HTMLElement
  private difficultModeEle: HTMLElement
  private currentMode: GameMode = 'simple'
  constructor() {
    this.simpleModeEle = document.getElementById('color-1')!
    this.advanceModeEle = document.getElementById('color-2')!
    this.difficultModeEle = document.getElementById('color-3')!
    this.bindEvent()
  }

  /**
   * 当前游戏的模式
   */
  get gameMode() {
    return this.currentMode
  }

  /**
   * 根据当前游戏模式和游戏进度,给出一个蛇移动的时间间隔数
   */
  get timeInterval() {
    let baseTime: number = 300
    if (this.gameMode == 'simple') {
      baseTime = 300
    } else if (this.gameMode == 'advance') {
      baseTime = 200
    } else if (this.gameMode == 'difficult') {
      baseTime = 100
    }
    if (Scoreboard.currentProgressNum >= 60) {
      return baseTime - baseTime * 0.3
    } else if (Scoreboard.currentProgressNum >= 50) {
      return baseTime - baseTime * 0.1
    } else if (Scoreboard.currentProgressNum >= 40) {
      return baseTime - baseTime * 0.2
    } else if (Scoreboard.currentProgressNum >= 30) {
      return baseTime - baseTime * 0.3
    } else if (Scoreboard.currentProgressNum >= 20) {
      return baseTime - baseTime * 0.4
    } else if (Scoreboard.currentProgressNum >= 10) {
      return baseTime - baseTime * 0.2
    } else if (Scoreboard.currentProgressNum >= 8) {
      return baseTime - baseTime * 0.3
    } else if (Scoreboard.currentProgressNum >= 6) {
      return baseTime - baseTime * 0.4
    } else if (Scoreboard.currentProgressNum >= 4) {
      return baseTime - baseTime * 0.2
    } else if (Scoreboard.currentProgressNum >= 2) {
      return baseTime - baseTime * 0.3
    } else if (Scoreboard.currentProgressNum >= 1) {
      return baseTime - baseTime * 0.4
    } else {
      return baseTime
    }
  }

  /**
   * 设置当前游戏模式
   * @param mode
   */
  private setMode(ev: Event, mode: GameMode) {
    // 只有游戏还没有开始才可以设置游戏模式
    if (
      GameControl.currentGameStatus == 'no_start' ||
      GameControl.currentGameStatus == 'prepare-start'
    ) {
      this.currentMode = mode
      InfoDialog.setVerVisible(
        true,
        '游戏模式选择',
        `你当前选择了${modeMap[mode]}模式！`
      )
    } else {
      //阻止单选框选中
      ev.preventDefault()
    }
  }

  /**
   * 绑定事件
   */
  private bindEvent() {
    this.simpleModeEle.addEventListener('click', (ev) => {
      ev.stopPropagation()
      this.setMode(ev, 'simple')
    })
    this.advanceModeEle.addEventListener('click', (ev) => {
      ev.stopPropagation()
      this.setMode(ev, 'advance')
    })
    this.difficultModeEle.addEventListener('click', (ev) => {
      ev.stopPropagation()
      this.setMode(ev, 'difficult')
    })
  }
}

export default new ModeBoard()
